/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tuoc;

import java.sql.ResultSet;
import java.util.Map;
import java.util.Vector;

/**
 *
 * @author apple
 */
public class store_model extends My_model{
    int id;
    int table_id;
    int product_id;
    int quantity;
    
//-------------------------------------------------------    FUNCTION
    public Vector get_elements()
    {
        ResultSet rs = this.executeSqlString("SELECT * FROM store");
        return this.to_domain_models(rs);
    }
    
    public Vector get_elements_by_table(String tableId)
    {
        String sql = String.format("SELECT * FROM store WHERE table_id = %s", tableId);
        ResultSet rs = this.executeSqlString(sql);
        return this.to_domain_models(rs);
    }
    
    public boolean checkExist(String productId,String tableId)
    {
        String sql = String.format("SELECT * FROM store WHERE product_id = %s AND table_id = %s", productId,tableId);
        ResultSet rs = this.executeSqlString(sql);
        if(this.to_domain_models(rs).size() > 0)
            return true;
        else
            return false;
    }
    
    public store_model get_element_by_table_product(String productId,String tableId)
    {
        String sql = String.format("SELECT * FROM store WHERE product_id = %s AND table_id = %s", productId,tableId);
        ResultSet rs = this.executeSqlString(sql);
        return this.to_domain_model(rs);
    }
    
    public store_model get_element_by_id(String id)
    {
        ResultSet rs = this.executeSqlString("SELECT * FROM store WHERE id = " + id);
        return this.to_domain_model(rs);
    }
    
    public void update_data(Map<String,String> data)
    {
        String sql = String.format("UPDATE store SET quantity = %s WHERE table_id = %s AND product_id = %s",
                data.get("quantity") + "",
                data.get("table_id"),
                data.get("product_id"));
        this.insertDatabase(sql);
    }
    
    public void insert_data(Map<String,String> data)
    {
        String sqlString = String.format("INSERT INTO store VALUES(NULL,%s,%s,%s)",
                (data.get("table_id") != null) ? data.get("table_id") : "NULL",
                (data.get("product_id") != null) ? data.get("product_id") : "NULL",
                (data.get("quantity") != null) ? data.get("quantity") : "NULL"
                );
        this.insertDatabase(sqlString);
    }
    
    public void delete_by_table(String tableId)
    {
        String sql = String.format("DELETE FROM store WHERE table_id = %s", tableId);
        this.insertDatabase(sql);
    }
    
    
//-------------------------------------------------------    PROPERTIES
    public String get_product_name()
    {
        products_model product = new products_model().get_element_by_id(String.format("%d", product_id));
        return product.name;
    }
    
    public Double get_product_price()
    {
        products_model product = new products_model().get_element_by_id(String.format("%d", product_id));
        return product.price;
    }
    
    public Double get_subTotal()
    {
        products_model product = new products_model().get_element_by_id(String.format("%d", product_id));
        return (product.price * quantity);
    }
    
    public String get_product_unit()
    {
        products_model product = new products_model().get_element_by_id(String.format("%d", product_id));
        return product.unit;
    }
    
//-------------------------------------------------------    TO MODEL
    
    public store_model to_domain_model(ResultSet rs)
    {
        store_model result = new store_model();
        try {
            if(rs.next())
            {
                result.id = rs.getInt("id");
                result.table_id = rs.getInt("table_id");
                result.product_id = rs.getInt("product_id");
                result.quantity = rs.getInt("quantity");
            }    
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    }
    
    public Vector to_domain_models(ResultSet rs)
    {
        Vector result = new Vector();
        try {
            while(rs.next())
            {
                store_model item = new store_model();
                item.id = rs.getInt("id");
                item.table_id = rs.getInt("table_id");
                item.product_id = rs.getInt("product_id");
                item.quantity = rs.getInt("quantity");
                
                result.add(item);
            }
            
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    }
}
